/* -----------------
* brief
* 1. this is code gen by tools
 */

package service

import (
	"errors"
	mchcore "github.com/chanxuehong/wechat/mch/core"
	"github.com/chanxuehong/wechat/mch/mmpaymkttransfers/promotion"
	"github.com/chanxuehong/wechat/mch/pay"
	"github.com/chanxuehong/wechat/mch/sandbox"
	paytools "github.com/chanxuehong/wechat/mch/tools"
	"saas_sys_base/common"
	"saas_sys_base/common/url_const"
	. "saas_sys_base/modules/wechat/models"
	"saas_sys_base/sysconfig"
)

type WxPayService struct {
	wechatSdkClient WechatSdkClient
	clientType WxClientType
}

func NewWxPayService(clientType WxClientType) *WxPayService {
	svc := WxPayService{
		clientType: clientType,
	}
	return &svc
}

func PayNotifyUrl(paytoUrl string) string {
	//"http://120.79.33.255:10000/wx_pay_callback" + "/pay_to_sys"
    return sysconfig.Config.Wechat.WX_PAY_NOTIFY_URL + url_const.WX_PAY_CALLBACK_URL + paytoUrl
}

func GetPayNotifyUrlByPayTo(payto common.PAY_TO) string {
	if payto == common.PAY_TO_SYS {
        return PayNotifyUrl(url_const.WX_PAY_TO_SYS_SUBPATH)
	} else if payto == common.PAY_TO_TENANT {
		return PayNotifyUrl(url_const.WX_PAY_TO_TENANT_SUBPATH)
	}

	return ""
}
// DEPRESSED
func (t *WxPayService) GetWeappMchClientByWhoAndPayTo2(appwho *common.Who, payto common.PAY_TO) (*mchcore.Client, error) {
	var mchclient *mchcore.Client
	var err error
	if payto == common.PAY_TO_SYS {
		mchclient, err = t.wechatSdkClient.GetWeappMchClientByWho(*common.SYS_WHO)
		if err != nil {
			return nil, err
		}
	} else {
		mchclient, err = t.wechatSdkClient.GetWeappMchClientByWho(*appwho)
		if err != nil {
			return nil, err
		}
	}
	return mchclient, nil
}

func (t *WxPayService) UnifiedorderByAppWho(appwho *common.Who, req *pay.UnifiedOrderRequest, payto common.PAY_TO) (resp *pay.UnifiedOrderResponse, paySignResult *pay.PaySignResult, err error) {
	//注意，这里小程序支付是用小程序关联的商户client
	mchclient, err := t.wechatSdkClient.GetMchClientByWhoWrapper(appwho, t.clientType)
	if err != nil {
		return nil, nil, err
	}

	req.NotifyURL = GetPayNotifyUrlByPayTo(payto)
    req.Attach = appwho.ToJson() //<==注意，放入Attach的字符串，在支付成功后，微信支付系统会附带在pay notify消息推送回来

	resp, err = pay.UnifiedOrder2(mchclient, req)
	if err != nil {
		return nil, nil, err
	}

	paySignResult = pay.GenPaySign(mchclient, resp.PrepayId)
	return resp, paySignResult, err
}

func (t *WxPayService) MicroPayByAppWho(appwho *common.Who, req *pay.MicroPayRequest) (resp *pay.MicroPayResponse, err error) {
	//注意，这里扫码是用公众号关联的商户client
	mchclient, err := t.wechatSdkClient.GetMchClientByWhoWrapper(appwho, t.clientType)
	if err == nil {
		return nil, errors.New("app未绑定商户号")
	}

	resp, err = pay.MicroPay2(mchclient, req)

	/*authCodeToOpenIdRequest := paytools.AuthCodeToOpenIdRequest{
		AuthCode:req.AuthCode,
	}
	paytools.AuthCodeToOpenId2(mchclient, &authCodeToOpenIdRequest)*/
	return resp, err
}

func (t *WxPayService) CloseOrderByAppWho(appwho *common.Who, req pay.CloseOrderRequest) (err error) {
	//注意，这里扫码是用公众号关联的商户client
	mchclient, err := t.wechatSdkClient.GetMchClientByWhoWrapper(appwho, t.clientType)
	if err == nil {
		return err
	}

	err = pay.CloseOrder2(mchclient, &req)
	return err
}

func (t *WxPayService) OrderQueryByAppWho(appwho *common.Who, req pay.OrderQueryRequest) (resp *pay.OrderQueryResponse, err error) {
	//注意，这里扫码是用公众号关联的商户client
	mchclient, err := t.wechatSdkClient.GetMchClientByWhoWrapper(appwho, t.clientType)
	if err == nil {
		return nil, err
	}

	resp, err = pay.OrderQuery2(mchclient, &req)
	return resp, err
}

func (t *WxPayService) RefundByAppWho(appwho *common.Who, req pay.RefundRequest) (resp *pay.RefundResponse, err error) {
	//注意，这里扫码是用公众号关联的商户client
	mchclient, err := t.wechatSdkClient.GetMchClientByWhoWrapper(appwho, t.clientType)
	if err != nil {
		return nil, err
	}

	resp, err = pay.Refund2(mchclient, &req)
	return resp, err
}

func (t *WxPayService) RefundQueryByAppWho(appwho *common.Who, req pay.RefundQueryRequest) (resp *pay.RefundQueryResponse, err error) {
	//注意，这里扫码是用公众号关联的商户client
	mchclient, err := t.wechatSdkClient.GetMchClientByWhoWrapper(appwho, t.clientType)
	if err == nil {
		return nil, err
	}

	resp, err = pay.RefundQuery2(mchclient, &req)
	return resp, err
}

func (t *WxPayService) ReverseByAppWho(appwho *common.Who, req pay.ReverseRequest) (resp *pay.ReverseResponse, err error) {
	//注意，这里扫码是用公众号关联的商户client
	mchclient, err := t.wechatSdkClient.GetMchClientByWhoWrapper(appwho, t.clientType)
	if mchclient == nil {
		return nil, err
	}

	resp, err = pay.Reverse2(mchclient, &req)
	return resp, err
}

func (t *WxPayService) GetSignKeyByAppWho(appwho common.Who, req sandbox.GetSignKeyRequest) (resp *sandbox.GetSignKeyResponse, err error) {
	//注意，这里扫码是用公众号关联的商户client
	mchclient, err := t.wechatSdkClient.GetMpMchClientByWho(appwho)
	if mchclient == nil {
		return nil, err
	}

	resp, err = sandbox.GetSignKey2(mchclient, &req)
	return resp, err
}

func (t *WxPayService) AuthCodeToOpenId(appwho *common.Who, req *paytools.AuthCodeToOpenIdRequest) (resp *paytools.AuthCodeToOpenIdResponse, err error) {
	//注意，这里扫码是用公众号关联的商户client
	mchclient, err := t.wechatSdkClient.GetMchClientByWhoWrapper(appwho, t.clientType)
	if mchclient == nil {
		return nil, err
	}

	resp, err = paytools.AuthCodeToOpenId2(mchclient, req)
	return resp, err
}

func (t *WxPayService) WithdrawByUserWho(userwho *common.Who, req *promotion.TransferReqPara) (resp *promotion.TransferRespData, err error) {
	t.clientType = WX_CLIENT_TYPE_WEAPP_MCH
	//注意，这里扫码是用公众号关联的商户client
	mchclient, err := t.wechatSdkClient.GetMchClientByWhoWrapper(userwho, t.clientType)
	if err != nil {
		return nil, err
	}

	resp, err = promotion.Transfers2(mchclient, req)
	return resp, err
}